<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>分校修改</title>
    <link href="../../layui/css/layui.css" rel="stylesheet">
    <link href="../../static/css/style.css" rel="stylesheet">
    <script src="../../static/js/jquery.min.js"></script>
    <script src="../../static/js/jquery.cookie.js"></script>
    <script src="../../layui/layui.js"></script>
    <script src="../../static/js/base.js"></script>
    <script src="../../static/js/ax.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="../../static/js/html5.js"></script>
    <script src="../../static/js/respond.js"></script>
    <![endif]-->

</head>
<body class="bg_w">

<div class="layui-fluid xyxx">
    <div class="layui-card-body">
        <form class="layui-form layui-form-pane" action="" lay-filter="component-form-element">
            <h5 class="lheight">上级机构</h5>
            <div class="layui-row layui-col-space10">
                <div class="layui-inline layui-col-lg3 layui-col-sm6">
                    <label class="layui-form-label" style="width: 150px;">总校</label>
                    <div class="layui-input-block" style="margin-left: 150px;">
                        <select id="gatherSchoolId" name="gatherSchoolId">

                        </select>
                    </div>
                </div>
            </div>

            <div class="layui-row layui-col-space10">
                <div class="layui-inline  layui-col-sm6">
                    <label class="layui-form-label" style="width: 150px;">分校名称</label>
                    <div class="layui-input-block" style="margin-left: 150px;">
                        <input type="text"  lay-verify="required|name" name="name" class="layui-input" id="name">
                        <input type="hidden" name="id" class="layui-input" id="id">
                    </div>
                </div>
                <div class="layui-col-sm6"><h6 class="pleft17 lheight">分校名称，汉字+字母+数字，100字符内</h6></div>
            </div>


            <div class="layui-row layui-col-space10">
                <div class="layui-inline  layui-col-sm6">
                    <label class="layui-form-label" style="width: 150px;">分校地址</label>
                    <div class="layui-input-block" style="margin-left: 150px;">
                        <input type="text"  lay-verify="required" name="address" class="layui-input" id="address">
                    </div>
                </div>
                <div class="layui-col-sm6">
                    <label class="layui-form-label" style="width: 150px;">经纬度</label>
                    <div class="layui-input-block" style="position: relative;margin-left: 150px;">
                        <input type="text" id="lonandlat" name="lonandlat"  lay-verify="required" readonly="readonly" placeholder="" autocomplete="off" class="layui-input" style=" margin-right: 10px; float: left;">
                        <input id="Button1" type="button" class="layui-btn add_btn" value="搜索"  style="position: absolute; right: 0;"  />
                    </div>
                </div>
            </div>
            <div class="layui-row layui-col-space10">
                <div class="layui-inline  layui-col-sm6">
                    <label class="layui-form-label" style="width: 150px;">联系人电话</label>
                    <div class="layui-input-block" style="margin-left: 150px;">
                        <input type="text" name="mobile" lay-verify="required|phone" class="layui-input" id="mobile">
                    </div>
                </div>
                <div class="layui-col-sm6">
                    <label class="layui-form-label" style="width: 150px;">联系人</label>
                    <div class="layui-input-block" style="margin-left: 150px;">
                        <input type="text" name="contacts" id="contacts" lay-verify="required" placeholder="" autocomplete="off" class="layui-input" style=" margin-right: 10px; float: left;">
                    </div>
                </div>
            </div>

            <!--模板-->


            <div class="layui-form-item layui-col-space10">
                <div class="layui-input-block">
                    <a id="btnClose" class="layui-btn layui-btn-primary btn-left">关闭</a>
                    <button class="layui-btn btn-left add_btn" lay-submit lay-filter="component-form-element">提交</button>

                </div>
            </div>
        </form>
    </div>
</div>


</body>

<script>

    var table;

    $(function () {
        var postData={"id":getQueryString("id")}
        ax.post({
            type: 'POST',
            url: schoolWebUrl+'/api/branch/info',
            contentType: "application/x-www-form-urlencoded",
            data: postData,
            success: function (r) {
                var data=r.data;
                console.log(data);
                //$("#branchSchoolId").val(data.branchSchoolId);
                $("#name").val(data.name);
                $("#gatherSchoolId").val(data.gatherSchoolId);
                $("#address").val(data.address);
                $("#lonandlat").val(data.longitude+","+data.latitude);
                $("#mobile").val(data.mobile);
                $("#contacts").val(data.contacts);
                $("#id").val(data.id);
            },

        });
    });

    layui.use(['table','form','laydate','layer','upload'], function() {
        table = layui.table;
        var form = layui.form,
            laydate = layui.laydate,
            layer=layui.layer,
            $ = layui.jquery,
            upload = layui.upload;

        //常规用法
        laydate.render({
            elem: '#activeTime'
            ,range: true
        });
        //获取总校
        $(function () {
            ax.get({
                url:schoolWebUrl+ '/api/gather/getGather',
                contentType: "application/x-www-form-urlencoded",
                data:"",
                success: function (r) {
                    $("#gatherSchoolId").append(' <option value='+r.data.id+'>'+r.data.name+'</option>');
                    form.render('select');
                },
            });
        });
        //地址失去焦点事件
        $("#address").blur(function(){
            var address= $("#address").val();
            $.ajax({
                type:'get',
                url:' https://restapi.amap.com/v3/geocode/geo?key=930ccb3f2c1982cbfbe33c328869ae40&address='+address,
                success: function (r) {
                    if(r.status==1){
                        if(JSON.stringify(r.geocodes)==='[]'){
                            layer.msg("地址信息错误");

                        }else {
                            $("#lonandlat").val(r.geocodes[0].location);
                        }
                    }else{
                        layer.alert("地址输入错误请重新输入");
                    }
                }
            });
        });

        //添加验证规则
        form.verify({
            name:[
                /^[a-zA-Z0-9\u4e00-\u9fa5]{1,100}$/,
                '分校名称必须是汉字+字母+数字，100字符内'
            ],
        });


        //监听提交
        form.on('submit(component-form-element)', function(data){
            var tude=data.field.lonandlat.split(",");
            data.field.longitude=tude[0];
            data.field.latitude=tude[1];
            if(data.field.branchSchoolId==null||data.field.branchSchoolId==""){
                data.field.parentId=data.field.gatherSchoolId
            }else{
                data.field.parentId=data.field.branchSchoolId
            }
            ax.post({
                type: 'POST',
                url: schoolWebUrl+'/api/branch/update',
                contentType: "application/x-www-form-urlencoded",
                data: data.field,
                success: function (r) {
                    layer.msg(r.message);
                    setTimeout(function () {
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                    },400)
                },
            });
            return false;
        });

        //关闭弹框

        $('#btnClose').on("click", function () {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        });

    });


</script>
</html>